﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IGGCore;

namespace IGGWindowApp.Panels
{
    public partial class PanelStats : UserControl
    {
        private bool alreadySet = false;

        public PanelStats()
        {
            InitializeComponent();
        }

        public void SetData(Scoreboard data)
        {
            if (alreadySet)
            {
                return;
            }
            alreadySet = true;

            // Off-1
            SetStats(tabOff1, "NL Batting Average", GetStat("Batting Average", data.StatsNLBatting), 6, 7);
            SetStats(tabOff1, "AL Batting Average", GetStat("Batting Average", data.StatsALBatting), 6, 121);
            SetStats(tabOff1, "NL OBP", GetStat("On-Base%", data.StatsNLBatting), 221, 7);
            SetStats(tabOff1, "AL OBP", GetStat("On-Base%", data.StatsALBatting), 221, 121);
            SetStats(tabOff1, "NL Home Runs", GetStat("Home Runs", data.StatsNLBatting), 442, 7);
            SetStats(tabOff1, "AL Home Runs", GetStat("Home Runs", data.StatsALBatting), 442, 121);
            SetStats(tabOff1, "NL RBI", GetStat("Runs Batted In", data.StatsNLBatting), 6, 234);
            SetStats(tabOff1, "AL RBI", GetStat("Runs Batted In", data.StatsALBatting), 6, 348);
            SetStats(tabOff1, "NL Slugging", GetStat("Slugging %", data.StatsNLBatting), 221, 234);
            SetStats(tabOff1, "AL Slugging", GetStat("Slugging %", data.StatsALBatting), 221, 348);
            SetStats(tabOff1, "NL OPS", GetStat("On-Base Plus Slugging", data.StatsNLBatting), 442, 234);
            SetStats(tabOff1, "AL OPS", GetStat("On-Base Plus Slugging", data.StatsALBatting), 442, 348);

            // Off-2
            SetStats(tabOff2, "NL Hits", GetStat("Hits", data.StatsNLBatting), 6, 7);
            SetStats(tabOff2, "AL Hits", GetStat("Hits", data.StatsALBatting), 6, 121);
            SetStats(tabOff2, "NL Bases on Balls", GetStat("Bases on Balls", data.StatsNLBatting), 221, 7);
            SetStats(tabOff2, "AL Bases on Balls", GetStat("Bases on Balls", data.StatsALBatting), 221, 121);
            SetStats(tabOff2, "NL Singles", GetStat("Singles", data.StatsNLBatting), 442, 7);
            SetStats(tabOff2, "AL Singles", GetStat("Singles", data.StatsALBatting), 442, 121);
            SetStats(tabOff2, "NL Doubles", GetStat("Doubles", data.StatsNLBatting), 6, 234);
            SetStats(tabOff2, "AL Doubles", GetStat("Doubles", data.StatsALBatting), 6, 348);
            SetStats(tabOff2, "NL Triples", GetStat("Triples", data.StatsNLBatting), 221, 234);
            SetStats(tabOff2, "AL Triples", GetStat("Triples", data.StatsALBatting), 221, 348);
            SetStats(tabOff2, "NL OPS+", GetStat("Adjusted OPS+", data.StatsNLBatting), 442, 234);
            SetStats(tabOff2, "AL OPS+", GetStat("Adjusted OPS+", data.StatsALBatting), 442, 348);

            // Off-3
            SetStats(tabOff3, "NL Stolen Bases", GetStat("Stolen Bases", data.StatsNLBatting), 6, 7);
            SetStats(tabOff3, "AL Stolen Bases", GetStat("Stolen Bases", data.StatsALBatting), 6, 121);
            SetStats(tabOff3, "NL Caught Stealing", GetStat("Caught Stealing", data.StatsNLBatting), 221, 7);
            SetStats(tabOff3, "AL Caught Stealing", GetStat("Caught Stealing", data.StatsALBatting), 221, 121);
            SetStats(tabOff3, "NL Runs Scored", GetStat("Runs Scored", data.StatsNLBatting), 442, 7);
            SetStats(tabOff3, "AL Runs Scored", GetStat("Runs Scored", data.StatsALBatting), 442, 121);
            SetStats(tabOff3, "NL At Bats", GetStat("At Bats", data.StatsNLBatting), 6, 234);
            SetStats(tabOff3, "AL At Bats", GetStat("At Bats", data.StatsALBatting), 6, 348);
            SetStats(tabOff3, "NL Strikeouts", GetStat("Strikeouts", data.StatsNLBatting), 221, 234);
            SetStats(tabOff3, "AL Strikeouts", GetStat("Strikeouts", data.StatsALBatting), 221, 348);
            SetStats(tabOff3, "NL Intentional Bases on Balls", GetStat("Intentional Bases on Balls", data.StatsNLBatting), 442, 234);
            SetStats(tabOff3, "AL Intentional Bases on Balls", GetStat("Intentional Bases on Balls", data.StatsALBatting), 442, 348);

            // Off-4
            SetStats(tabOff4, "NL WAR Position Players", GetStat("WAR Position Players", data.StatsNLBatting), 6, 7);
            SetStats(tabOff4, "AL WAR Position Players", GetStat("WAR Position Players", data.StatsALBatting), 6, 121);
            SetStats(tabOff4, "NL Extra Base Hits", GetStat("Extra Base Hits", data.StatsNLBatting), 221, 7);
            SetStats(tabOff4, "AL Extra Base Hits", GetStat("Extra Base Hits", data.StatsALBatting), 221, 121);
            SetStats(tabOff4, "NL Hit By Pitch", GetStat("Hit By Pitch", data.StatsNLBatting), 442, 7);
            SetStats(tabOff4, "AL Hit By Pitch", GetStat("Hit By Pitch", data.StatsALBatting), 442, 121);
            SetStats(tabOff4, "NL Double Plays Grounded Into", GetStat("Double Plays Grounded Into", data.StatsNLBatting), 6, 234);
            SetStats(tabOff4, "AL Double Plays Grounded Into", GetStat("Double Plays Grounded Into", data.StatsALBatting), 6, 348);
            SetStats(tabOff4, "NL Sacrifice Flies", GetStat("Sacrifice Flies", data.StatsNLBatting), 221, 234);
            SetStats(tabOff4, "AL Sacrifice Flies", GetStat("Sacrifice Flies", data.StatsALBatting), 221, 348);
            SetStats(tabOff4, "NL Games Played", GetStat("Games Played", data.StatsNLBatting), 442, 234);
            SetStats(tabOff4, "AL Games Played", GetStat("Games Played", data.StatsALBatting), 442, 348);

            // Pitch-1
            SetStats(tabPitch1, "NL ERA", GetStat("Earned Run Average", data.StatsNLPitching), 6, 7);
            SetStats(tabPitch1, "AL ERA", GetStat("Earned Run Average", data.StatsALPitching), 6, 121);
            SetStats(tabPitch1, "NL Wins", GetStat("Wins", data.StatsNLPitching), 221, 7);
            SetStats(tabPitch1, "AL Wins", GetStat("Wins", data.StatsALPitching), 221, 121);
            SetStats(tabPitch1, "NL WHIP", GetStat("Walks & Hits per IP", data.StatsNLPitching), 442, 7);
            SetStats(tabPitch1, "AL WHIP", GetStat("Walks & Hits per IP", data.StatsALPitching), 442, 121);
            SetStats(tabPitch1, "NL Saves", GetStat("Saves", data.StatsNLPitching), 6, 234);
            SetStats(tabPitch1, "AL Saves", GetStat("Saves", data.StatsALPitching), 6, 348);
            SetStats(tabPitch1, "NL Strikeouts", GetStat("Strikeouts", data.StatsNLPitching), 221, 234);
            SetStats(tabPitch1, "AL Strikeouts", GetStat("Strikeouts", data.StatsALPitching), 221, 348);
            SetStats(tabPitch1, "NL Win-Loss %", GetStat("Win-Loss %", data.StatsNLPitching), 442, 234);
            SetStats(tabPitch1, "AL Win-Loss %", GetStat("Win-Loss %", data.StatsALPitching), 442, 348);

            // Pitch-2
            SetStats(tabPitch2, "NL WAR", GetStat("WAR for Pitchers", data.StatsNLPitching), 6, 7);
            SetStats(tabPitch2, "AL WAR", GetStat("WAR for Pitchers", data.StatsALPitching), 6, 121);
            SetStats(tabPitch2, "NL Complete Games", GetStat("Complete Games", data.StatsNLPitching), 221, 7);
            SetStats(tabPitch2, "AL Complete Games", GetStat("Complete Games", data.StatsALPitching), 221, 121);
            SetStats(tabPitch2, "NL Shutouts", GetStat("Shutouts", data.StatsNLPitching), 442, 7);
            SetStats(tabPitch2, "AL Shutouts", GetStat("Shutouts", data.StatsALPitching), 442, 121);
            SetStats(tabPitch2, "NL Innings Pitched", GetStat("Innings Pitched", data.StatsNLPitching), 6, 234);
            SetStats(tabPitch2, "AL Innings Pitched", GetStat("Innings Pitched", data.StatsALPitching), 6, 348);
            SetStats(tabPitch2, "NL Games Started", GetStat("Games Started", data.StatsNLPitching), 221, 234);
            SetStats(tabPitch2, "AL Games Started", GetStat("Games Started", data.StatsALPitching), 221, 348);
            SetStats(tabPitch2, "NL Adjusted ERA+", GetStat("Adjusted ERA+", data.StatsNLPitching), 442, 234);
            SetStats(tabPitch2, "AL Adjusted ERA+", GetStat("Adjusted ERA+", data.StatsALPitching), 442, 348);

            // Pitch-3
            SetStats(tabPitch3, "NL Wild Pitches", GetStat("Wild Pitches", data.StatsNLPitching), 6, 7);
            SetStats(tabPitch3, "AL Wild Pitches", GetStat("Wild Pitches", data.StatsALPitching), 6, 121);
            SetStats(tabPitch3, "NL Losses", GetStat("Losses", data.StatsNLPitching), 221, 7);
            SetStats(tabPitch3, "AL Losses", GetStat("Losses", data.StatsALPitching), 221, 121);
            SetStats(tabPitch3, "NL Hit By Pitch", GetStat("Hit By Pitch", data.StatsNLPitching), 442, 7);
            SetStats(tabPitch3, "AL Hit By Pitch", GetStat("Hit By Pitch", data.StatsALPitching), 442, 121);
            SetStats(tabPitch3, "NL Earned Runs", GetStat("Earned Runs", data.StatsNLPitching), 6, 234);
            SetStats(tabPitch3, "AL Earned Runs", GetStat("Earned Runs", data.StatsALPitching), 6, 348);
            SetStats(tabPitch3, "NL Games Finished", GetStat("Games Finished", data.StatsNLPitching), 221, 234);
            SetStats(tabPitch3, "AL Games Finished", GetStat("Games Finished", data.StatsALPitching), 221, 348);
            SetStats(tabPitch3, "NL Adj. Pitching Wins", GetStat("Adj. Pitching Wins", data.StatsNLPitching), 442, 234);
            SetStats(tabPitch3, "AL Adj. Pitching Wins", GetStat("Adj. Pitching Wins", data.StatsALPitching), 442, 348);

            // Pitch-4
            SetStats(tabPitch4, "NL Hits per 9 IP", GetStat("Hits per 9 IP", data.StatsNLPitching), 6, 7);
            SetStats(tabPitch4, "AL Hits per 9 IP", GetStat("Hits per 9 IP", data.StatsALPitching), 6, 121);
            SetStats(tabPitch4, "NL Bases On Balls per 9 IP", GetStat("Bases On Balls per 9 IP", data.StatsNLPitching), 221, 7);
            SetStats(tabPitch4, "AL Bases On Balls per 9 IP", GetStat("Bases On Balls per 9 IP", data.StatsALPitching), 221, 121);
            SetStats(tabPitch4, "NL Strikeouts per 9 IP", GetStat("Strikeouts per 9 IP", data.StatsNLPitching), 442, 7);
            SetStats(tabPitch4, "AL Strikeouts per 9 IP", GetStat("Strikeouts per 9 IP", data.StatsALPitching), 442, 121);
            SetStats(tabPitch4, "NL Strikeouts / Base On Balls", GetStat("Strikeouts / Base On Balls", data.StatsNLPitching), 6, 234);
            SetStats(tabPitch4, "AL Strikeouts / Base On Balls", GetStat("Strikeouts / Base On Balls", data.StatsALPitching), 6, 348);
            SetStats(tabPitch4, "NL Home Runs per 9 IP", GetStat("Home Runs per 9 IP", data.StatsNLPitching), 221, 234);
            SetStats(tabPitch4, "AL Home Runs per 9 IP", GetStat("Home Runs per 9 IP", data.StatsALPitching), 221, 348);
            SetStats(tabPitch4, "NL Win Probability Added", GetStat("Win Probability Added (WPA)", data.StatsNLPitching), 442, 234);
            SetStats(tabPitch4, "AL Win Probability Added", GetStat("Win Probability Added (WPA)", data.StatsALPitching), 442, 348);

            // Def-1
            SetStats(tabDef1, "NL Putouts", GetStat("Putouts", data.StatsNLFielding), 6, 7);
            SetStats(tabDef1, "AL Putouts", GetStat("Putouts", data.StatsALFielding), 6, 121);
            SetStats(tabDef1, "NL Assists", GetStat("Assists", data.StatsNLFielding), 221, 7);
            SetStats(tabDef1, "AL Assists", GetStat("Assists", data.StatsALFielding), 221, 121);
            SetStats(tabDef1, "NL Errors Committed", GetStat("Errors Committed", data.StatsNLFielding), 442, 7);
            SetStats(tabDef1, "AL Errors Committed", GetStat("Errors Committed", data.StatsALFielding), 442, 121);
            SetStats(tabDef1, "NL Stolen Bases", GetStat("Stolen Bases", data.StatsNLFielding), 6, 234);
            SetStats(tabDef1, "AL Stolen Bases", GetStat("Stolen Bases", data.StatsALFielding), 6, 348);
            //SetStats(tabDef1, "NL Home Runs per 9 IP", GetStat("Home Runs per 9 IP", data.StatsNLFielding), 221, 234);
            //SetStats(tabDef1, "AL Home Runs per 9 IP", GetStat("Home Runs per 9 IP", data.StatsALFielding), 221, 348);
            //SetStats(tabDef1, "NL Win Probability Added", GetStat("Win Probability Added (WPA)", data.StatsNLFielding), 442, 234);
            //SetStats(tabDef1, "AL Win Probability Added", GetStat("Win Probability Added (WPA)", data.StatsALFielding), 442, 348);

            // Def-2
        }

        /// <summary>
        /// Set stats on the form
        /// </summary>
        private void SetStats(TabPage page, string label, StatItem item, int startX, int startY)
        {
            Label statTitle = new Label();
            statTitle.AutoSize = true;
            statTitle.Font = new System.Drawing.Font("Franklin Gothic Book", 9.75F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            statTitle.Location = new System.Drawing.Point(startX, startY);
            statTitle.Size = new System.Drawing.Size(46, 17);
            statTitle.Text = label;
            page.Controls.Add(statTitle);

            for (int i = 0; i < 5; i++)
            {
                startY += 17;
                Label statLabel = new Label();
                statLabel.AutoSize = true;
                statLabel.Font = new System.Drawing.Font("Franklin Gothic Book", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                statLabel.Location = new System.Drawing.Point(startX, startY);
                statLabel.Size = new System.Drawing.Size(71, 17);
                statLabel.Text = item.Names[i] + ", " + item.Teams[i].ToUpper();
                page.Controls.Add(statLabel);

                Label valueLabel = new Label();
                valueLabel.AutoSize = true;
                valueLabel.Font = new System.Drawing.Font("Franklin Gothic Book", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                valueLabel.Location = new System.Drawing.Point(startX + 146, startY);
                valueLabel.Size = new System.Drawing.Size(71, 17);
                valueLabel.Text = item.Values[i];
                page.Controls.Add(valueLabel);
            }

        }

        /// <summary>
        /// Find a particular stat amongst the stats
        /// </summary>
        private StatItem GetStat(string subject, List<StatItem> items)
        {
            StatItem result = null;
            foreach (StatItem item in items)
            {
                if (item.Subject == subject)
                {
                    result = item;
                    break;
                }
            }
            return result;
        }

        private void tabOff1_Click(object sender, EventArgs e)
        {

        }
    }


}
